package com.dk.controller;

import com.dk.pojo.CodeRule;
import com.dk.pojo.Location;
import com.dk.result.PageResult;
import com.dk.result.Result;
import com.dk.result.ResultConstant;
import com.dk.service.CodeRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("codeRule")
@Api(tags = "编码生成规则接口文档")
public class CodeRuleController {

    @Autowired
    private CodeRuleService codeRuleService;

    @GetMapping("getCodeRuleInfo")
    @ApiOperation("编码生成规则模糊查询和分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数"),
            @ApiImplicitParam(name = "ruleName", value = "规则名称"),
            @ApiImplicitParam(name = "ruleCode", value = "规则编码"),
            @ApiImplicitParam(name = "enableFlag", value = "是否启用:Y是/N否"),
    })
    public Result getCodeRuleInfo(@RequestParam(defaultValue = "1")Integer pageNum,
                                  @RequestParam(defaultValue = "5") Integer pageSize,
                                  String ruleName, String ruleCode,String enableFlag){
        PageResult<CodeRule> result = codeRuleService.getCodeRuleInfo(pageNum,pageSize,ruleName,ruleCode,enableFlag);
        return new Result(ResultConstant.CODE,ResultConstant.MESSAGE,result);
    }

    @PostMapping("addCodeRule")
    @ApiOperation("编码生成规则增加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ruleCode",value = "规则编码",required = true),
            @ApiImplicitParam(name = "ruleName",value = "规则名称",required = true),
            @ApiImplicitParam(name = "ruleDesc",value = "描述",required = false),
            @ApiImplicitParam(name = "maxLength",value = "最大长度",required = false),
            @ApiImplicitParam(name = "isPadded",value = "是否补齐:Y是/N否",required = true),
            @ApiImplicitParam(name = "paddedChar",value = "补齐字符",required = false),
            @ApiImplicitParam(name = "paddedMethod",value = "补齐方式:L左/R右",required = false),
            @ApiImplicitParam(name = "enableFlag",value = "是否启用:Y是/N否",required = true),
            @ApiImplicitParam(name = "remark",value = "备注",required = false),
            @ApiImplicitParam(name = "token", value = "登录后返回的token串",required = true)
    })
    public Result addCodeRule(CodeRule codeRule,@RequestHeader String token){
        if(token == null || "".equals(token)){
            return new Result(ResultConstant.TOKEN_CODE, ResultConstant.TOKEN_MESSAGE);
        }
        return codeRuleService.addCodeRule(codeRule,token);
    }

    @PutMapping("updateCodeRule")
    @ApiOperation("编码生成规则修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ruleId",value = "规则编码id",required = true),
            @ApiImplicitParam(name = "ruleCode",value = "规则编码"),
            @ApiImplicitParam(name = "ruleName",value = "规则名称"),
            @ApiImplicitParam(name = "ruleDesc",value = "描述"),
            @ApiImplicitParam(name = "maxLength",value = "最大长度"),
            @ApiImplicitParam(name = "isPadded",value = "是否补齐:Y是/N否"),
            @ApiImplicitParam(name = "paddedChar",value = "补齐字符"),
            @ApiImplicitParam(name = "paddedMethod",value = "补齐方式:L左/R右"),
            @ApiImplicitParam(name = "enableFlag",value = "是否启用:Y是/N否"),
            @ApiImplicitParam(name = "remark",value = "备注"),
            @ApiImplicitParam(name = "token", value = "登录后返回的token串",required = true)
    })
    public Result updateCodeRule(CodeRule codeRule,@RequestHeader String token){
        if(token == null || "".equals(token)){
            return new Result(ResultConstant.TOKEN_CODE, ResultConstant.TOKEN_MESSAGE);
        }
        return codeRuleService.updateCodeRule(codeRule,token);
    }

    @DeleteMapping("deleteCodeRule")
    @ApiOperation("编码生成规则删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ruleId", value = "编码生成规则id",required = true)
    })
    public Result deleteCodeRule(Long ruleId){
        codeRuleService.deleteCodeRule(ruleId);
        return new Result(ResultConstant.CODE,ResultConstant.MESSAGE);
    }

    @GetMapping("selectByRuleCode")
    public List<CodeRule> selectByRuleCode(@RequestParam String ruleCode){
        return codeRuleService.selectByRuleCode(ruleCode);
    }
}